﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.DataModel.Form;
using ServiceApplication.Dao.Form;
using System.Data;
using System.Xml.Linq;
using CommonLibrary.Common;
using System.IO;

namespace ServiceApplication.Service
{
    public class FormService
    {
        FormDao formDao = new FormDao();
        FormFieldDao formFieldDao = new FormFieldDao();

        #region 表单
        public FormEntity AddForm(FormEntity formEntity)
        {
            int Form_id = formDao.AddEntity(formEntity);
            formEntity.form_id = Form_id;
            foreach (FormFieldEntity ffe in formEntity.formfieldEntityList)
            {
                ffe.form_id = Form_id;
                ffe.formfield_id = formFieldDao.addEntity(ffe);
            }
            return formEntity;
        }

        public void UpdateForm(FormEntity formEntity)
        {
            formDao.UpdateEntity(formEntity);
        }

        public void DeleteFrom(int form_id)
        {
            formDao.DleteEntity(form_id);
        }

        /// <summary>
        /// 如果该表单不没有工作流参数则formfieldEntityList=null
        /// </summary>
        /// <param name="form_id"></param>
        /// <returns></returns>
        public FormEntity QueryFormEntity(int form_id)
        {
            FormEntity formEntity = formDao.QueryEntity(form_id);
            formEntity.formfieldEntityList = formFieldDao.QueryEntitys(form_id, null);
            return formEntity;
        }

        /// <summary>
        /// 结果无返回NULL
        /// </summary>
        /// <param name="form_id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<FormFieldEntity> QueryFormFieldEntitys(int form_id, string type)
        {
            return formFieldDao.QueryEntitys(form_id, type);
        }

        /// <summary>
        ///  user_id=-1 表示查询所有
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public DataTable QueryFormDataTable(int user_id)
        {
            DataTable dt = formDao.QueryFormDataTable(user_id);
            return dt;
        }

        /// <summary>
        /// 查询没有对应wf模板的表单
        /// </summary>
        /// <returns></returns>
        public DataTable QueryFreeForm()
        {
            DataTable dt = formDao.QueryFreeForm();
            return dt;
        }

        /// <summary>
        /// 查询所有表单
        /// </summary>
        /// <returns></returns>
        public DataTable QueryAllForm()
        {
            DataTable dt = formDao.QueryAllForm();
            return dt;
        }

        public FormEntity QueryFormEntityByWfinsId(string wfinsId)
        {
            return formDao.QueryFormEntityByWfinsId(wfinsId);
        }

        #endregion

        #region 表单字段
        /// <summary>
        /// 查询表单字段
        /// </summary>
        /// <param name="form_id">表单</param>
        /// <param name="formfield_id"></param>
        /// <returns></returns>
        public FormFieldEntity QueryFormfield(int formfield_id)
        {
            FormFieldEntity formFieldEntity = formFieldDao.QueryEntity(formfield_id);
            return formFieldEntity;
        }
        #endregion
    }
}
